package org.mogoo.haydn;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mogoo.haydn.tools.ConnDB;



import com.google.gson.stream.JsonReader;

public class MyServlet extends HttpServlet
{
	private ResultSet rs = null;
	private Connection ct = new ConnDB().getConn();
	private PreparedStatement ps = null; 
	/**
	 * Constructor of the object.
	 */
	public MyServlet()
	{
		super();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		response.setContentType("text/html");
		PrintWriter writer = response.getWriter();
		//writer.print("hello");
		String json_grid = request.getParameter("json_grid");
		String json_grid_trajectory = request.getParameter("json_grid_trajectory");
		
		writer.print("OK");
		System.out.println("Data received from phone!");
		System.out.println(json_grid);
		String counts = "";
		String grid_no = "";
		String grid_x = "";
		String grid_y = "";
		String timeOfStay = "";
		try
		{
			// table grid_trajectory
			JsonReader reader = new JsonReader(new StringReader(json_grid));
			reader.beginArray();
			while(reader.hasNext())
			{
				reader.beginObject();
				while(reader.hasNext())
				{
					String tagName = reader.nextName();
					if(tagName.equals("counts"))
					{
						counts = reader.nextString();
					}
					else if(tagName.equals("grid_no"))
					{
						grid_no = reader.nextString();
					}
					else if(tagName.equals("grid_x"))
					{
						grid_x = reader.nextString();
					}
					else if(tagName.equals("grid_y"))
					{
						grid_y = reader.nextString();
					}
					else if(tagName.equals("timeOfStay"))
					{
						timeOfStay = reader.nextString();
					}
				}
				reader.endObject();
				
				String sql = "select grid_no, total_stay_of_time, counts from grid where grid_no=" + grid_no +";";
				ps = ct.prepareStatement(sql);
				rs = ps.executeQuery();
				if(rs.next())
				{
					double tosTmp = rs.getDouble(2);
					long countsTmp = rs.getLong(3);
					sql = " update grid set  counts = " + (countsTmp + Double.parseDouble(counts)) + ", total_stay_of_time = " + (tosTmp + Long.parseLong(timeOfStay)) + " where grid_no = " + grid_no + ""; 
					ps = ct.prepareStatement(sql);
					ps.executeUpdate();
				}
				else
				{
					sql = " insert into grid (grid_no, grid_x, grid_y, total_stay_of_time, counts) values (" + grid_no + ", " + grid_x + ", " + grid_y + ", " + timeOfStay + ", " + counts + ");";
					ps = ct.prepareStatement(sql);
					ps.executeUpdate();
				}
			}
			reader.endArray();
			System.out.println("update table grid complete!");
			
			// table grid_trajectory
			grid_no = "";
			grid_x = "";
			grid_y = "";
			String time_ = "";
			reader = new JsonReader(new StringReader(json_grid_trajectory));
			reader.beginArray();
			while(reader.hasNext())
			{
				reader.beginObject();
				while(reader.hasNext())
				{
					String tagName = reader.nextName();
					if(tagName.equals("time_"))
					{
						time_ = reader.nextString();
					}
					else if(tagName.equals("grid_no"))
					{
						grid_no = reader.nextString();
					}
					else if(tagName.equals("grid_x"))
					{
						grid_x = reader.nextString();
					}
					else if(tagName.equals("grid_y"))
					{
						grid_y = reader.nextString();
					}
				}
				reader.endObject();
				
				String sql = "insert into grid_trajectory (time_, grid_x, grid_y, grid_no) values (" + time_ + ", " + grid_x + ", " + grid_y + ", " + grid_no + ");";
				ps = ct.prepareStatement(sql);
				ps.executeUpdate();
			}
			reader.endArray();
			System.out.println("update table grid_trajectory complete!");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				rs.close();
				ps.close();
				ct.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		doGet(request, response);
	}

}
